package com.bjc.core.controller;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.bjc.base.bean.ResultVO;
import com.bjc.base.core.ResultGenerator;
import com.bjc.base.core.annotation.PermissionPath;
import com.bjc.core.bean.request.RichTextReqVO;
import com.bjc.core.bean.response.RichTextVO;
import com.bjc.core.service.IRichTextService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 富文本控制器
 * 
 * @author 许自辉
 * @date 2017年7月5日 下午5:23:15
 */

@SpringBootConfiguration
@RestController
@RequestMapping("api/cloud")
@PermissionPath("系统管理/富文本")
@Api(value = "richText-api", tags = { "富文本相关接口" })
public class RichTextController {

	@Autowired
	private IRichTextService richTextService;

	@ApiOperation("分页查询富文本")
	@RequiresPermissions("richText:findPage")
	@GetMapping("richText/findPage")
	public ResultVO getLookupPage(@RequestParam("pageNum") int pageNum, @RequestParam("pageSize") int pageSize,
			RichTextReqVO reqVO) {
		PageHelper.startPage(pageNum, pageSize);
		List<RichTextVO> richTextVOs = richTextService.getList(reqVO);
		return ResultGenerator.getSuccess(new PageInfo<RichTextVO>(richTextVOs));
	}

	@ApiOperation("添加富文本")
	@RequiresPermissions("richText:create")
	@PostMapping("richText/create")
	public ResultVO createLookup(@RequestBody RichTextReqVO reqVOs) {
		int count = richTextService.insert(reqVOs);
		if (count == -1) {
			return ResultGenerator.getFail("该富文本已经存在！");
		}
		return ResultGenerator.getInsertSuccess(count);
	}

	@ApiOperation("更新富文本")
	@RequiresPermissions("richText:update")
	@PutMapping("richText/update")
	public ResultVO updateLookup(@RequestBody RichTextReqVO reqVOs) {
		int count = richTextService.update(reqVOs);
		return ResultGenerator.getUpdateSuccess(count);
	}

	@ApiOperation("删除富文本")
	@RequiresPermissions("richText:deleteList")
	@DeleteMapping("richText/delete/{id}")
	public ResultVO deleteLookup(@PathVariable("id") Integer id) {
		int count = richTextService.deleteById(id);
		return ResultGenerator.getDeleteSuccess(count);
	}
}
